#include "rel.h"

/**
 * Parses Command Line for Sender
 *
 * @param argc number of arguments
 * @param argv argument strings
 * @param fileName char buffer to hold fileName
 * @param ipAddress char buffer to hold host address
 * @param port receiver port number
 */
int commandLineSend(char argc, char * argv[], char * fileName, char * ipAddress, unsigned short * port){
  
  // Check to ensure we got three arguments: fileName, IP, and port
  if (argc != 4) {
    error("Please enter a fileName followed by an IP Address and Port Number");
  }
  
  else {    

    // Get fileName
	strcpy(fileName,argv[1]);
        
    // Check to ensure fileName length is not greater than 20
    if (strlen(fileName) > 21){
      error("fileName maximum length of 20 has been exceeded");
    }
	  
	// Check to ensure file exists within directory
	FILE * checkFile;
	checkFile = fopen(fileName, "rb");
	
	if(checkFile == NULL) {
	  error("Can't send a file that doesn't exist!");
	}
	
	else { 
	  if (debug) {
		printf("%s exists in the directory\n", fileName);
	  }
		
	  fclose(checkFile);
	}
	
	  // Get IP Address of receiver
    strcpy(ipAddress,argv[2]);
    
    // Check to see if valid port entered as third argument
    * port = checkPort(argv[3]);       
  }

  if (debug) {
    printf("\nfileName: %s\n", fileName);
    printf("ADDRESS: %s\n", ipAddress);
    printf("PORT: %d\n\n", *port);
  }
  return 0;
}

/**
 * Parses Command Line for Receiver
 *
 * @param port Receiver port to bind on
 */
int commandLineRecv(int argc, char * argv[], unsigned short * port){

  if (argc != 2) {
    error("Please enter a Port Number");
  }
  
  else {
    //Check to see if valid port entered as first argument
    * port = checkPort(argv[1]);    
  }
  
  if (debug) {
    printf("PORT: %d\n", *port);
  }
  
  return 0;
}